$(function(){
    function Sidebar(contentHeader){
        this.contentHeader=contentHeader;
        this.selected=this.contentHeader.data('sidebar-select');
        this.title=[];
        this.init();
    }
    Sidebar.prototype={
        init:function(){
            this.activeSidebar();
            this.renderBreadcrumb();
           // this.setPageTitle();
        },
        activeSidebar:function(){
            for(var i=0;i<this.selected.length;i++) {
                var sidebarS = "$('.sidebar')";
                var queryS = ".children('ul').children('li').eq(" + this.selected[0] + ")";
                for (var j = 0; j <i; j++) {
                    var s = ".children('ul').children('li').eq(" + this.selected[j+1] + ")";
                    queryS += s;
                }
                var op = ".addClass('active')";
                var evalS = sidebarS + queryS + op;
                eval(evalS);
            }
        },
        renderBreadcrumb:function(){
            var self=this;
            $('.main-sidebar').find('.active').each(function(){
                var title={};
                title.text=$.trim($(this).children('a').text());
                title.href=$(this).children('a').attr('href');
                title.icon=$(this).children('a').find('i').prop('outerHTML');
                self.title.push(title);
            });
            var lastTitle={};
            lastTitle.text=$.trim(this.contentHeader.find('h1').contents().filter(function (index, content) {
                return content.nodeType === 3;
            }).text());//不包括小标题
            //lastTitle.href="window.location.reload()";
            var isSame=(lastTitle.text==this.title[this.title.length-1].text);
            if(!isSame){
                this.title.push(lastTitle)
            }
            var ol=$('<ol>',{
                class:'breadcrumb'
            });
            for(var i=0;i<this.title.length;i++){
                var text=this.title[i].text||'';
                var icon=this.title[i].icon||'';
                var href=this.title[i].href||'#';
                var a="<a href="+href+">"+icon+text+"</a>";
                var li=$('<li>');
                if(i==this.title.length-1){
                    a=text;
                    li.addClass('active');
                }
                li.html(a);
                ol.append(li);
            }
            this.contentHeader.append(ol);
        },
        setPageTitle:function(){
            if(!document.title){
                document.title=this.title[this.title.length-1].text;
            }
        }
    }
    new Sidebar($('.content-header'));

});